package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.lantekpre.LantekNestPlate;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportPreNestResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportPrePlateResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LantekNestPlateMapper extends SyMapper<LantekNestPlate> {

    @Select("SELECT o.mater_code AS constructCode, plate.texture AS texture, plate.norm AS thickness, MIN( plan.plate_length ) AS standardPlateLength, MIN( plan.plate_width ) AS standardPlateWidth, " +
            "COUNT( DISTINCT plate.stock_list_no ) AS planAmount, SUM( part.net_weight * part.quantity ) AS partWeight, SUM( part.area * part.quantity ) AS partArea, " +
            "SUM( part.rectangle_area * part.quantity ) AS rectangleArea, nest.split_group_code AS workGroup, plate.pre_version AS versionCode, SUM( DISTINCT excess.weight ) AS usedPlateWeight " +
            "FROM tb_third_lantek_nest_plate plate LEFT JOIN tb_third_lantek_nest_part part ON plate.nest_id = part.nest_id AND plate.pre_version = part.pre_version " +
            "LEFT JOIN tb_third_lantek_push_plan plan ON part.nest_plan_id = plan.nest_plan_id AND part.pre_version = plan.pre_version LEFT JOIN tb_aps_work_detail awd ON part.nest_plan_id = awd.id " +
            "LEFT JOIN tb_order o ON awd.order_id = o.id LEFT JOIN tb_third_lantek_nest_nest nest ON plate.nest_id = nest.nest_id AND plate.pre_version = nest.pre_version " +
            "LEFT JOIN tb_third_lantek_nest_excess excess ON plate.nest_id = excess.nest_id AND plate.pre_version = excess.pre_version " +
            " ${ew.customSqlSegment} ")
    List<ReportPreNestResVo> preNestReport(@Param(Constants.WRAPPER) QueryWrapper<LantekNestPlate> qw);

    @Select("SELECT DISTINCT nest.nest_id AS nestId, plate.stock_list_no AS plateCode, plate.texture AS texture, plate.norm AS thickness, bps.length AS plateLength, bps.width AS plateWidth, " +
            "( SELECT COUNT(*) FROM tb_third_lantek_nest_part np WHERE np.plate_code = plate.stock_list_no AND np.pre_version = plate.pre_version ) AS partKinds, " +
            "( SELECT SUM( hrnp.quantity ) FROM tb_third_lantek_nest_part hrnp WHERE hrnp.plate_code = plate.stock_list_no AND hrnp.pre_version = plate.pre_version ) AS partCount, " +
            "plate.rate AS rate, nest.nest_date AS nestDateTime, " +
            "( SELECT move1.cut_length FROM tb_third_lantek_nest_move move1 WHERE move1.nest_id = plate.nest_id AND move1.move_type = 1 ) AS cutLength1, " +
            "( SELECT move2.cut_length FROM tb_third_lantek_nest_move move2 WHERE move2.nest_id = plate.nest_id AND move2.move_type = 2 ) AS cutLength2, " +
            "( SELECT move3.total_cut_time FROM tb_third_lantek_nest_move move3 WHERE move3.nest_id = plate.nest_id AND move3.move_type = 1 ) AS cutTime " +
            "FROM tb_third_lantek_nest_plate plate LEFT JOIN tb_third_lantek_nest_nest nest ON plate.nest_id = nest.nest_id AND plate.pre_version = nest.pre_version " +
            "LEFT JOIN tb_base_plate_standard bps ON plate.texture = bps.texture AND plate.norm = bps.height " +
            "LEFT JOIN tb_third_lantek_nest_part part ON plate.nest_id = part.nest_id AND plate.pre_version = part.pre_version " +
            "LEFT JOIN tb_aps_work_detail awd ON part.nest_plan_id = awd.id LEFT JOIN tb_order o ON awd.order_id = o.id " +
            " ${ew.customSqlSegment} ")
    Page<ReportPrePlateResVo> preNestPlate(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<LantekNestPlate> qw);

}
